package com.smdk.dsminio.config;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smdk.dsminio.redis.RedisUtil;
import com.smdk.dsminio.vo.MultipartFileParam;
import feign.Request;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import static java.lang.String.format;
public class FeignSupportConfig {
    @Autowired
    private RedisUtil REDISUTILS;


    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }

    //路由重构
    @Bean
    public RequestInterceptor cloudContextInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                String url = template.url();
                if (url.contains("$SERVER_ID")) {
                    url = url.replace("$SERVER_ID", route());
                    template.uri(url);
                }
                if (url.startsWith("//")) {
                    url = "http:" + url;
                    template.target(url);
                    template.uri("");
                }

            }

            /**
             * 上传文件路由分发算法
             * @return
             */
            private CharSequence route() {
                MultipartFileParam multipartFileParam = null;
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = attributes.getRequest();
                Enumeration<String> bodyNames = request.getParameterNames();
                if (bodyNames != null) {
                    Map map = new HashMap();
                    while (bodyNames.hasMoreElements()) {
                        String name = bodyNames.nextElement();
                        String values = request.getParameter(name);
                        map.put(name, values);
                    }
                    multipartFileParam = BeanUtil.mapToBean(map, MultipartFileParam.class, false);
                }
                String serverId = REDISUTILS.get(multipartFileParam.getMd5());
                if (serverId != null) {
                    return serverId;
                } else {
                    String nodeInfo = REDISUTILS.get("nodeInfo");
                    JSONArray array = JSONObject.parseArray(nodeInfo);
                    JSONObject jsonObject = array.getJSONObject(0);
                    REDISUTILS.setex(multipartFileParam.getMd5(), jsonObject.getString("id"), 60 * 60 * 60);
                    return jsonObject.getString("id");
                }
            }
        };
    }
}